<?php
/** 爱阅读
 * Created by PhpStorm.
 * User: denh
 * Date: 16/5/30
 * Time: 下午1:38
 */
class IreadController extends BaseController {

    function __construct($m = DEFAULT_CONTROLLER, $a = DEFAULT_METHOD) {
        parent::__construct($m, $a);
    }

    //爱阅读主页
    function index(){
        $do_zixun = new Do咨询信息();
        $do_zixun_detail = new Do咨询详情();
        $zixun_list = $do_zixun->fields("id,date_format(添加时间,'%Y-%m-%d') as 添加时间")->where(array('发送'=>'已发送'))->orderby(' id desc')->offset(0)->limit(10)->rows();
        foreach($zixun_list as $key => $item){
            $zixun_list[$key]['item'] = $do_zixun_detail->where(array('咨询id'=>$item['id']))->orderby('排序 asc')->rows();

        }
        $ajax_index_url = $this->url('iread','index_ajax');
        require $this->template;
    }
    function index_ajax(){
        $do_zixun = new Do咨询信息();
        $do_zixun_detail = new Do咨询详情();
        $pageNum = intval($_REQUEST['page']);
        if($pageNum<=0){
            $pageNum = 1;
        }
        $start = ($pageNum-1)*10;
        $res = $do_zixun->fields("id,date_format(添加时间,'%Y-%m-%d') as 添加时间")->where(array('发送'=>'已发送'))->orderby('id desc ')->offset($start)->limit(10)->rows();

        if(count($res)<=0){
            echo "zero";
          //  break;
        }
        foreach($res as $key => $item){
            $res_zixun_detail = $do_zixun_detail->where(array('咨询id'=>$item['id']))->orderby('排序 asc')->rows();
            $res[$key]['item'] = $res_zixun_detail;
        }
        $str="";
        foreach($res as $k => $v){
            $str.="<div class='LoveRead_time'><div>{$v['添加时间']}</div></div><dl>";
            foreach($v['item'] as $key => $val){
                if($key == 0){
                    $str .= "<a href='".$val['文章url']."'><dt><div class='img' style='background:url(".$val['图片url'].") no-repeat center;background-size:cover;'><div><h1>".$val['标题']."</h1></div></div></dt></a>";
                }else{
                    $str .= "<a href='".$val['文章url']."'><dd><table><tr><td class='LRtxt'>{$val['标题']}</td><td><div class='img' style=background:url(".$val['图片url'].")  no-repeat center;background-size:cover;'></div></td></tr></table></dd></a>";
                }
            }
            $str.= "</dl>";
        }
      echo $str;

    }
}